package icedot.library.common.ui;

import icedot.library.common.R;
import icedot.library.common.utils.CommonFun;
import icedot.library.common.utils.StringUtils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomTitle extends LinearLayout
{
	private TextView 			_btnLeft;
	private TextView 			_btnRight;
	private TextView 			_textTitle;
	private RelativeLayout 		_layoutItem;
	
	private LinearLayout		_layoutNoNetwork;   //无网络
	private TextView			_textNetworkDesc;
	
	public CustomTitle(Context context)
	{
		super(context);
		initView(context);
	}
	
	public CustomTitle(Context context, AttributeSet attrs)
	{
		super(context,attrs);
		initView(context);
	}
	
	private void initView(Context context)
	{
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);  
		inflater.inflate(R.layout.custom_title, this);  
		_layoutItem = (RelativeLayout)findViewById(R.id.title_layout_item);
		_btnLeft = (TextView)findViewById(R.id.title_text_left);
		_btnRight = (TextView)findViewById(R.id.title_text_right);
		_textTitle = (TextView)findViewById(R.id.title_text_title);
		if(_layoutItem !=null)
		{
			_layoutItem.setVisibility(View.VISIBLE);
		}
		
		_layoutNoNetwork = (LinearLayout)findViewById(R.id.title_layout_nonetwork);
		_textNetworkDesc = (TextView)findViewById(R.id.title_text_networkdesc);
		if(_layoutNoNetwork != null)
		{
			_layoutNoNetwork.setVisibility(View.GONE);
		}
	}
	
	@Override
	public void setBackgroundColor(int color)
	{
		_layoutItem.setBackgroundColor(color);
	}
	
	public void setTitleVisible(int show)
	{
		_layoutItem.setVisibility(show);
	}
	
	public void setNoNetworkVisible(int show,String content)
	{
		_layoutNoNetwork.setVisibility(show);
		if(!StringUtils.isNullOrEmpty(content))
		{
			_textNetworkDesc.setText(content);
		}
	}
	
	public TextView getNoNetworkDesc()
	{
		return _textNetworkDesc;
	}
	
	public TextView getLeftButton()
	{
		return _btnLeft;
	}
	public TextView getRightButton()
	{
		return _btnRight;
	}
	public TextView getTitle()
	{
		return _textTitle;
	}
	
	public void setTitle(String title)
	{
		if(_textTitle == null)
			return;
		_textTitle.setText(title);
	}
	
	public void setLeftButton(String content,int leftID)
	{
		if(_btnLeft == null)
			return;
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
				_btnLeft.getLayoutParams();
		params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
		params.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
		_btnLeft.setLayoutParams(params);
		_btnLeft.setVisibility(View.VISIBLE);
		_btnLeft.setText(content);
		if(leftID != 0)
		{
			_btnLeft.setCompoundDrawablesWithIntrinsicBounds(leftID, 0, 0, 0);
			_btnLeft.setCompoundDrawablePadding(10);
		}
	}
	public void setLeftButton(int imgRSID)
	{
		if(_btnLeft == null)
			return;
		_btnLeft.setVisibility(View.VISIBLE);
		_btnLeft.setBackgroundResource(imgRSID);
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
				_btnLeft.getLayoutParams();
		params.height = CommonFun.dip2px(getContext(), 30);
		params.width = params.height;
		_btnLeft.setLayoutParams(params);
		_btnLeft.setText("");
	}
	public void setRightButton(int imgRSID)
	{
		if(_btnRight == null)
			return;
		_btnRight.setVisibility(View.VISIBLE);
		_btnRight.setBackgroundResource(imgRSID);
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
				_btnRight.getLayoutParams();
		params.height = CommonFun.dip2px(getContext(), 30);
		params.width = params.height;
		_btnRight.setLayoutParams(params);
		_btnRight.setText("");
	}
	
	public void setRightButton(String content,int rightID)
	{
		if(_btnRight == null)
			return;
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
				_btnRight.getLayoutParams();
		params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
		params.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
		_btnRight.setLayoutParams(params);
		_btnRight.setVisibility(View.VISIBLE);
		_btnRight.setText(content);
		if(rightID != 0)
		{
			_btnRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, rightID, 0);
			_btnRight.setCompoundDrawablePadding(10);
		}
	}
	
	public void hideLeftButton()
	{
		if(_btnLeft == null)
			return;
		_btnLeft.setVisibility(View.GONE);
	}
	
	public void hideRightButton()
	{
		if(_btnRight == null)
			return;
		_btnRight.setVisibility(View.GONE);
	}
}
